package com.newly.center.business.compenont;

import com.alibaba.csp.ahas.shaded.com.alibaba.acm.shaded.com.google.gson.Gson;
import com.newly.center.business.service.CheckRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @anthor sbq
 * @date 2021/12/1 14:54
 */
//@Component
public class RealTimeTask {

    @Autowired
    private CheckRecordService checkRecordService;

    /**
     * cron表达式：Seconds Minutes Hours DayofMonth Month DayofWeek [Year] 每10秒扫描一次推送消息
     */
    @Scheduled(cron = "0/10 * * * * ?")
    public void push(){
        CopyOnWriteArraySet<WebSocketServer> webSocketServerSet = WebSocketServer.getWebSocketServerSet();
        webSocketServerSet.forEach(t -> {
            try {
                t.SendMessage(new Gson().toJson(checkRecordService.getRealTimeData(10)));
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

}
